#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    string convert(string s, int numRows) {
        int cur = 0;
        vector<string> v;
        while (true)
        {
            string tmp;
            for (int i = 0;i < numRows;++i)
            {
                if (cur == s.size())
                {
                    while (tmp.size() != numRows)
                    {
                        tmp.push_back(' ');
                    }
                    v.push_back(tmp);
                    goto back;
                }
                tmp.push_back(s[cur++]);
            }
            v.push_back(tmp);
            tmp.clear();
            for (int j = 1, index = 1;j <= numRows - 2;++j, ++index)
            {
                for (int i = 0;i < numRows;++i)
                {
                    if (index == i)
                    {
                        if (cur == s.size())
                        {
                            while (tmp.size() != numRows)
                            {
                                tmp.push_back(' ');
                            }
                            v.push_back(tmp);
                            goto back;
                        }
                        tmp.push_back(s[cur++]);
                    }
                    else
                        tmp.push_back(' ');
                }
                std::reverse(tmp.begin(), tmp.end());
                v.push_back(tmp);
                tmp.clear();
            }
        }

    back:
        s.clear();
        for (int i = 1, j = 0;i <= numRows;++i, ++j)
        {
            for (int k = 0;k < v.size();k++)
            {
                if (v[k][j] != ' ')
                    s.push_back(v[k][j]);
            }
        }
        return s;
    }
};